package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.*;
import tech.aistar.service.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Attention")
public class AttentionController {
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IConcernService concernService;
    @Autowired
    private IHouseService houseService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private ISeeRoomTrendService seeRoomTrendService;
    @Autowired
    private IImageService iImageService;
    @Autowired
    private IHouseService iHouseService;
    @GetMapping("/attentionHouse/{tenantId}")
    public String select(HttpServletRequest request,@PathVariable("tenantId") Integer tenantId) {
//        String tenantId = request.getParameter("tenantId");
        List<Concern> concernList = concernService.selectBytenantId(Integer.valueOf(tenantId));
        request.setAttribute("concernList",concernList);
        List<House> houseList = new ArrayList<>();
        for (int i = 0; i < concernList.size(); i++) {
            House house = houseService.selectByhouseId(concernList.get(i).getHouseId());
            houseList.add(house);
        }
        request.setAttribute("houseList",houseList);
        Tenant tenant = tenantService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("tenant", tenant);

        List<Message> messageStates = messageService.selectByState(tenant.getRegisterId());
        request.setAttribute("messageSize",messageStates.size());
        List<SeeRoomTrend> seeRoomTrends=seeRoomTrendService.selectThree();
        int i=0;
        for (SeeRoomTrend seeRoomTrend : seeRoomTrends) {
            i++;
            request.setAttribute("image"+i, iImageService.selectOneByHouseId(seeRoomTrend.getHouseId()));
            request.setAttribute("house"+i,iHouseService.selectByhouseId(seeRoomTrend.getHouseId()));
        }
        return "/personal_homepage/tenant/Attention";
    }

    @PostMapping("/delAtt")
    public @ResponseBody
    Result deleteById(Integer id) {
        int count = concernService.deleteById(id);
        if (count == 1) {
            Result result = new Result("200", "取消成功");
            return result;
        } else {
            Result result = new Result("500", "取消失败");
            return result;
        }
    }
}
